package com.troila;

import com.troila.service.MqttSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.UUID;

@Component
public class MockMessageRunner implements CommandLineRunner {

    private MqttSendService mqttSendService;

    @Autowired
    public void setMqttSendService(MqttSendService mqttSendService) {
        this.mqttSendService = mqttSendService;
    }

    @Override
    public void run(String... args) throws Exception {
        Flux.interval(Duration.ofSeconds(1))
                .map(tick -> LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli())
                .subscribe(temp ->
                        mqttSendService.sendToMqtt("troilabox/234",
                                "troilabox/123",
                                UUID.randomUUID().toString(),
                                0,
                                String.valueOf(temp)));
        Thread.currentThread().join();
    }
}
